#day1 数组运算与可视化
import numpy as np
import matplotlib.pyplot as plt

def create_random_matrix(rows, cols, low=1, high=10):
    """创建随机整数矩阵"""
    return np.random.randint(low, high, size=(rows, cols))

def show_basic_stats(arr):
    """输出并返回数组的基础统计信息"""
    arr_sum = np.sum(arr)
    arr_mean = np.mean(arr)
    arr_max = np.max(arr)
    arr_min = np.min(arr)
    print(f"Sum: {arr_sum}, Mean: {arr_mean}, Max: {arr_max}, Min: {arr_min}")
    return arr_sum, arr_mean, arr_max, arr_min

def matrix_operations(mat1, mat2):
    """展示矩阵基本运算"""
    print("Matrix 1:\n", mat1)
    print("Matrix 2:\n", mat2)
    print("Dot product:\n", np.dot(mat1, mat2))
    print("Add:\n", np.add(mat1, mat2))
    print("Subtract:\n", np.subtract(mat1, mat2))
    print("Multiply:\n", np.multiply(mat1, mat2))
    print("Divide:\n", np.divide(mat1, mat2))

def visualize_matrix(matrix, title="Matrix Visualization"):
    """用imshow可视化矩阵"""
    plt.figure(figsize=(6, 5))
    plt.imshow(matrix, cmap='viridis', interpolation='none')
    plt.colorbar(label='Value')
    plt.title(title)
    plt.xlabel('Column')
    plt.ylabel('Row')
    # plt.savefig('matrix_visualization.png')
    plt.show()

# 主流程入口
if __name__ == "__main__":
    arr1 = create_random_matrix(3, 3)
    print("Array arr1:\n", arr1)
    show_basic_stats(arr1)
    
    matrix1 = np.array([[1, 2], [3, 4]])
    matrix2 = np.array([[5, 6], [7, 8]])
    matrix_operations(matrix1, matrix2)
    
    # 可视化矩阵 
    visualize_matrix(matrix1, "Matrix1 Visualization") 


